package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
//@SuppressWarnings("all")
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;


    //todo 1. 新增或修改公寓信息
    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //todo 1. 判断条件 声明	-->		必须在前
        boolean isUpdate = apartmentSubmitVo.getId() != null;
/*
        //todo 根据apartmentSubmitVo 获取 省 市 区信息
        String provinceName = apartmentSubmitVo.getProvinceName();
        String cityName = apartmentSubmitVo.getCityName();
        String districtName = apartmentSubmitVo.getDistrictName();
        //todo 获取的信息传给新增或修改省 市 区信息*/

        //todo 2. 新增或修改公寓信息  对公寓本身操作没有影响
        this.saveOrUpdate(apartmentSubmitVo);
        //获取公寓id
        Long apartmentId = apartmentSubmitVo.getId();
        //todo 3. 判断当前功能是新增还是修改
        if (isUpdate) {
            //todo 如果是修改，现将公寓所对应的图片信息、配套、标签、杂费删除
            //todo 删除公寓对应省信息


            //todo 3.1 删除公寓所对应的图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoApartmentIdWrapper = new LambdaQueryWrapper<>();
            graphInfoApartmentIdWrapper
                    // itemType 是 公寓  apartmentId 是 公寓id
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId, apartmentId);
            graphInfoService.remove(graphInfoApartmentIdWrapper);

            //todo 3.2 删除公寓所对应的配套信息
            LambdaQueryWrapper<ApartmentFacility> facilityApartmentIdWrapper = new LambdaQueryWrapper<>();
            facilityApartmentIdWrapper
                    .eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityService.remove(facilityApartmentIdWrapper);

            //todo 3.3 删除公寓所对应的标签信息
            LambdaQueryWrapper<ApartmentLabel> labelApartmentIdWrapper = new LambdaQueryWrapper<>();
            labelApartmentIdWrapper
                    .eq(ApartmentLabel::getApartmentId, apartmentId);
            apartmentLabelService.remove(labelApartmentIdWrapper);

            //todo 3.4 删除公寓所对应的杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueApartmentIdWrapper = new LambdaQueryWrapper<>();
            feeValueApartmentIdWrapper
                    .eq(ApartmentFeeValue::getApartmentId, apartmentId);
            apartmentFeeValueService.remove(feeValueApartmentIdWrapper);
        }

        //todo 4. 新增公寓配套信息
        //todo 4.1 新增公寓所对应的图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        //isEmpty(graphVoList)判断 : null或者长度为0
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentId);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            //saveBatch : 批量添加数据
            graphInfoService.saveBatch(graphInfoList);
        }
        //todo 4.2 新增公寓所对应的配套信息
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)) {
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                //根据@Builder 注解  生成builder方法
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentId)
                        .facilityId(facilityId).build();
                facilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }
        //todo 4.3. 新增公寓所对应标签信息
        List<Long> labelInfoIdList = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelInfoIdList)) {
            ArrayList<ApartmentLabel> labelList = new ArrayList<>();
            for (Long labelId : labelInfoIdList) {
                ApartmentLabel apartmentLabel = ApartmentLabel
                        .builder().apartmentId(apartmentId)
                        .labelId(labelId).build();
                labelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(labelList);
        }
        //todo 4.4. 公寓所对应杂费信息
        List<Long> feeValueIdList = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIdList)) {
            ArrayList<ApartmentFeeValue> feeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIdList) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue
                        .builder().apartmentId(apartmentId)
                        .feeValueId(feeValueId).build();
                feeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(feeValueList);
        }
    }

    //todo 2. 分页查询公寓信息
    @Override
    public IPage<ApartmentItemVo> selectApartmentItemVoPage(IPage<ApartmentItemVo> page, ApartmentQueryVo
            queryVo) {
        return apartmentInfoMapper.selectApartmentItemVoPage(page, queryVo);
    }

    //todo 3. 获取公寓详情
    @Override
    public ApartmentDetailVo selectApartmentDetailVoById(Long apartmentId) {
        //todo 1. 查询公寓信息 this代指当前要查询的对象
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        // 判断公寓信息是否存在
        if (apartmentInfo == null) {
            return null;
        }
        //todo 2. 查询公寓所对应的图片信息
        List<GraphVo> graphVoList = graphInfoMapper.selectGraphVoListByItemTypeAndItemId(ItemType.APARTMENT, apartmentId);
        //todo 3. 查询公寓所对应的配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectFacilityInfoListByApartmentId(apartmentId);
        //todo 4. 查询公寓所对应的标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectLabelInfoListByApartmentId(apartmentId);
        //todo 5. 查询公寓所对应的杂费信息
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectFeeValueVoListByApartmentId(apartmentId);
        //todo 6. 封装返回结果
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //todo 6.1 将公寓信息复制到公寓详情中 属性复制 前提 ： 属性名一致
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);
        //todo 6.2 将图片信息、配套信息、标签信息、杂费信息封装到公寓详情中
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        //todo 7. 返回结果
        return apartmentDetailVo;
    }

    //todo 4. 删除公寓信息
    @Override
    public void removeApartmentInfoById(Long apartmentId) {
        //todo 1. 判断公寓下是否有房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper
                .eq(RoomInfo::getApartmentId, apartmentId)
                .eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED);
        Long count = roomInfoService.count(roomInfoLambdaQueryWrapper);
        if (count > 0) {
            //todo 如果有房间，则不能删除 : 抛出异常   -->   不会执行删除操作
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //todo 2. 删除公寓所对应的图片、配套、标签、杂费信息
        //todo 2.1 删除公寓所对应的图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoApartmentIdWrapper = new LambdaQueryWrapper<>();
        graphInfoApartmentIdWrapper
                .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                .eq(GraphInfo::getItemId, apartmentId);
        graphInfoService.remove(graphInfoApartmentIdWrapper);

        //todo 2.2 删除公寓所对应的配套信息
        LambdaQueryWrapper<ApartmentFacility> facilityApartmentIdWrapper = new LambdaQueryWrapper<>();
        facilityApartmentIdWrapper
                .eq(ApartmentFacility::getApartmentId, apartmentId);
        apartmentFacilityService.remove(facilityApartmentIdWrapper);

        //todo 2.3 删除公寓所对应的标签信息
        LambdaQueryWrapper<ApartmentLabel> labelApartmentIdWrapper = new LambdaQueryWrapper<>();
        labelApartmentIdWrapper
                .eq(ApartmentLabel::getApartmentId, apartmentId);
        apartmentLabelService.remove(labelApartmentIdWrapper);

        //todo 2.4 删除公寓所对应的杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> feeValueApartmentIdWrapper = new LambdaQueryWrapper<>();
        feeValueApartmentIdWrapper
                .eq(ApartmentFeeValue::getApartmentId, apartmentId);
        apartmentFeeValueService.remove(feeValueApartmentIdWrapper);
        //todo 3. 删除公寓信息
        removeById(apartmentId);
    }

}




